<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="学校" prop="schoolName">
        <el-input v-model="queryParams.schoolName" readonly="readonly" @click.native="openSelectSchool(1)"
          placeholder="请输入学校" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="付款" prop="payState">
        <el-select v-model="queryParams.payState" placeholder="请选择付款状态" clearable>
          <el-option v-for="dict in dict.type.common_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作时间" prop="dateArray">
        <el-date-picker clearable type="daterange" format="yyyy-MM-dd" v-model="queryParams.dateArray"
          value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:00', '23:59:59']" placeholder="请选择签约日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="学生姓名" prop="stuName">
        <el-input v-model="queryParams.stuName" placeholder="请输入学生姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学生联系电话" prop="stuTel">
        <el-input v-model="queryParams.stuTel" placeholder="请输入学生联系电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="validFlag">
        <el-select v-model="queryParams.validFlag" placeholder="请选择订单状态" clearable>
          <el-option v-for="dict in dict.type.order_valid_flag" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['school:studentorder:report']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="schoolinfoList">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="学校全称" align="center" prop="schoolName" />
      <el-table-column label="联系人" align="center" prop="contacts" />
      <el-table-column label="联系电话" align="center" prop="tel" />
      <el-table-column label="招生人数" align="center" prop="stuNum" />
      <el-table-column label="学生" align="center" prop="stuName" />
      <el-table-column label="付款" align="center" prop="payState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_state" :value="scope.row.payState" />
        </template>
      </el-table-column>
      <el-table-column label="费用类型" align="center" prop="renewType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.stu_member_type" :value="scope.row.renewType" />
        </template>
      </el-table-column>
      <el-table-column label="订单类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderType==1" type="info">新建</el-tag>
          <el-tag v-if="scope.row.orderType==2" type="warning">续费</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="计算方式" align="center" prop="feeType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.feeType==1" type="info">费率</el-tag>
          <el-tag v-if="scope.row.feeType==2" type="warning">固定金额</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单金额(元)" align="center" prop="orderAmount" />
      <el-table-column label="会员有效期-开始" align="center" prop="startDate" />
      <el-table-column label="会员有效期-结束" align="center" prop="expireDate" />
      <el-table-column label="操作时间" align="center" prop="gmtCreate" />
      <el-table-column label="订单状态" align="center" prop="validFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_valid_flag" :value="scope.row.validFlag" />
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div v-if="scope.row.payState==1 && scope.row.validFlag!=4">
            <el-button size="mini" type="text" v-if="scope.row.payState==1" icon="el-icon-edit"
              @click="handleUpdate(scope.row,'update')" v-hasPermi="['school:studentorder:edit']">修改</el-button>
            <el-button size="mini" type="text" v-if="scope.row.payState==1" icon="el-icon-delete"
              @click="handleDelete(scope.row)" v-hasPermi="['school:studentorder:abandonment']">废弃</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>


    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 选择校区 -->
    <SelectSchool ref="selectSchool" :open="selectFlag" @selected-data="selectSchoolData"
      @close-select-window="closeSelectSchool" />

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="续费期限" prop="renewType">
          <el-radio-group v-model="form.renewType" @input="switchMemberType">
            <el-radio-button v-for="dict in dict.type.stu_member_type" :key="dict.value" :label="dict.value"
              border>{{dict.label}}</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="开始日期">
          <el-date-picker v-model="form.startDate" @change="selectValidTo" value-format="yyyy-MM-dd"
            date-format="yyyy-MM-dd" type="date" placeholder="选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到期日期">
          <el-date-picker v-model="form.newValidTo" value-format="yyyy-MM-dd HH:mm:ss" date-format="yyyy-MM-dd"
            type="date" readonly placeholder="结束日期">
          </el-date-picker>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" @keyup.enter.native="submitForm"
          :loading="submitLoading">{{ submitLoading ? '提交中...' : '确定' }}</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    listStudentorder,
    getStudentorder,
    abandonment,
    updateStudentorder
  } from "@/api/bus_web/school/studentorder";
  import SelectSchool from "../school/schoolinfo/select.vue"
  import {
    isEmpty
  } from "@/utils/validate"
  import {
    isNumberStr
  } from '@/utils/index'
  import {
    mapState
  } from 'vuex';
  import store from "@/store";
  export default {
    name: "School_renew_order_report_detail",
    dicts: ['stu_member_type', 'common_state', 'order_valid_flag'],
    components: {
      SelectSchool
    },
    data() {
      return {
        options: [],
        // 是否显示代理选择
        selectFlag: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 是否显示弹出层
        open: false,
        submitLoading: false,
        // 总条数
        total: 0,
        // 学校管理表格数据
        schoolinfoList: [],
        // 弹出层标题
        title: "",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          schoolIdArray: null,
          schoolName: null,
          stuName: null,
          stuTel: null,
          dateArray: []
        },
        form: {
          id: null,
          stuId: null,
          orderType: null,
          feeType: null,
          renewType: null,
          orderAmount: null,
          schoolId: null,
          startDate: null, // 👈 显式声明
          newValidTo:null,
          expireDate: null,
          gmtCreate: null,
          deleted: null,
          gmtModify: null,
          createBy: null,
          updateBy: null
        },
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    computed: {
      ...mapState({
        school: state => state.selectSchool.school,
        schoolSelectModel: state => state.selectSchool.schoolSelectModel,
      })
    },
    methods: {
      /* 切换日期*/
      switchMemberType(value) {
        let days = 0
        if (1 == value) {
          days = 92
        } else if (2 == value) {
          days = 183
        } else if (3 == value) {
          days = 275
        } else if (4 == value) {
          days = 366
        }

        if (!isEmpty(this.form.startDate)) {
          const newDate = new Date(this.form.startDate)
          newDate.setDate(new Date(this.form.startDate).getDate() + days)
          this.$set(this.form, 'newValidTo', this.parseTime(newDate));
        }

      },
      selectValidTo(time) {

        if (!isEmpty(this.form.renewType)) {
          let value = this.form.renewType
          let days = 0
          if (1 == value) {
            days = 92
          } else if (2 == value) {
            days = 183
          } else if (3 == value) {
            days = 275
          } else if (4 == value) {
            days = 366
          }

          const newDate = new Date(time)
          newDate.setDate(new Date(time).getDate() + days)
          this.$set(this.form, 'newValidTo', this.parseTime(newDate));
        }
      },
      /** 查询学校管理列表 */
      getList() {
        this.loading = true

        if (this.queryParams.dateArray.length > 0) {
          this.queryParams.startTime = this.queryParams.dateArray[0]
          this.queryParams.endTime = this.queryParams.dateArray[1]
        }
        listStudentorder(this.queryParams).then(response => {
          this.schoolinfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.schoolIdArray = null
        this.queryParams.startTime = null
        this.queryParams.endTime = null
        this.handleQuery();
      },
      //选择校区
      openSelectSchool(selectVal) {
        this.selectFlag = true
        // selectVal =1 查询模式 =2 创建、修改模式
        this.$store.dispatch('selectSchool/cleanSchool')
        this.$store.dispatch('selectSchool/setSchoolSelectModel', selectVal)
      },
      closeSelectSchool() {
        this.selectFlag = false
      },
      //确定校区
      selectSchoolData() {
        this.closeSelectSchool()
        if (this.schoolSelectModel == 2) {
          this.form.schoolName = this.school[0].schoolName
          this.form.schoolId = this.school[0].id
          this.$refs.form.clearValidate(['schoolName']);
        } else {
          this.queryParams.schoolName = this.school.map(item => item.schoolName).join(',')
          this.queryParams.schoolIdArray = this.school.map(item => item.id)
        }
        this.$store.dispatch('selectSchool/cleanSchool')
      },

      /** 导出按钮操作 */
      handleExport() {
        this.download('school/studentorder/export', {
          ...this.queryParams
        }, `订单明细_${new Date().getTime()}.xlsx`)
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getStudentorder(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改学生续费订单";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.submitLoading = true
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateStudentorder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            this.submitLoading = false
          }
        });
      },
      /** 废弃按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认废弃订单编号为"' + ids + '"的数据项？').then(function() {
          return abandonment(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("废弃成功");
        }).catch(() => {});
      }, // 表单重置
      reset() {
        this.form = {
          id: null,
          stuId: null,
          orderType: null,
          feeType: null,
          renewType: null,
          orderAmount: null,
          schoolId: null,
          startDate: null,
          expireDate: null,
          gmtCreate: null,
          deleted: null,
          gmtModify: null,
          createBy: null,
          updateBy: null
        };
        this.resetForm("form");
      }, // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
    }
  };
</script>

<style>
</style>
